<?php

namespace app\api\service\igetui;

use think\Exception;
use think\Loader;
use think\Log;

//Loader::import('GetuiSDK.IGt.Push');
//Loader::import('GetuiSDK.igetui/IGt.AppMessage');
//Loader::import('GetuiSDK.igetui/IGt.TagMessage');
//Loader::import('GetuiSDK.igetui/IGt.APNPayload');
//Loader::import('GetuiSDK.igetui/template/IGt.BaseTemplate');
//Loader::import('GetuiSDK.IGt.Batch');
//Loader::import('GetuiSDK.igetui/utils/AppConditions');
//Loader::import('GetuiSDK.igetui/template/notify/IGt.Notify');
//Loader::import('GetuiSDK.igetui/IGt.MultiMedia');
//Loader::import('GetuiSDK.payload/VOIPPayload');

require_once EXTEND_PATH . '/GetuiSDK/IGt.Push.php';
require_once EXTEND_PATH . '/GetuiSDK/IGt.Batch.php';
require_once EXTEND_PATH . '/GetuiSDK/igetui/IGt.AppMessage.php';
require_once EXTEND_PATH . '/GetuiSDK/igetui/IGt.TagMessage.php';
require_once EXTEND_PATH . '/GetuiSDK/igetui/IGt.APNPayload.php';
require_once EXTEND_PATH . '/GetuiSDK/igetui/IGt.MultiMedia.php';
require_once EXTEND_PATH . '/GetuiSDK/igetui/utils/AppConditions.php';
require_once EXTEND_PATH . '/GetuiSDK/igetui/template/IGt.BaseTemplate.php';
require_once EXTEND_PATH . '/GetuiSDK/igetui/template/notify/IGt.Notify.php';
require_once EXTEND_PATH . '/GetuiSDK/payload/VOIPPayload.php';

class Getui
{

    /**
     * @var string
     */
    protected $app_id;

    /**
     * @var IGtPush
     */
    protected $igt;

    /**
     * Whether or not send offline message.
     *
     * @var bool;
     */
    protected $is_offline;

    /**
     * Offline message expire time.
     *
     * @var int
     */
    protected $offline_expire_time;

    /**
     * Network type.
     *
     * @var int
     */
    protected $network_type;


    public function __construct()
    {
        $config = config('getui');
        $this->app_id = $config['app_id'];
        $this->igt = new \IGeTui($config['host'], $config['app_key'], $config['master_secret']);
        $this->is_offline = $config['push']['is_offline'];
        $this->offline_expire_time = $config['push']['offline_expire_time'];
        $this->network_type = $config['push']['network_type'];
    }

    // 服务端推送接口，支持三个接口推送
    //1.PushMessageToSingle接口：支持对单个用户进行推送
    //2.PushMessageToList接口：支持对多个用户进行推送，建议为50个用户
    //3.pushMessageToApp接口：对单个应用下的所有用户进行推送，可根据省份，标签，机型过滤推送
    //
    //消息模版：
    // 1.NotificationTemplate:点击通知打开应用模板
    // 2.LinkTemplate:点击通知打开网页模板
    // 3.NotyPopLoadTemplate：点击通知弹框下载模板
    // 4.TransmissionTemplate：透传消息模版

    /**
     * 单推接口
     */
    public function pushMessageToSingle($data)
    {
        // 解析数据
        $template_type = $data['template_type'];
        $template_data = $data['template_data'];
        $cid = $data['cid'];
        $is_off_line = isset($data['template_data']['is_offline']) ? (bool)$data['template_data']['is_offline'] : $this->is_offline;
        $offline_expire_time = isset($data['template_data']['is_offline']) ? (int)$data['template_data']['offline_expire_time'] * 1000 * 3600 : $this->offline_expire_time * 1000 * 3600;
        $network_type = isset($data['template_data']['network_type']) ? (int)$data['template_data']['network_type'] : $this->network_type;

        // 消息模版
        $getui_template = new GetuiTemplate($template_type, $template_data);
        $template = $getui_template->getTemplate();

        // 个推信息体
        $message = new \IGtSingleMessage();
        $message->set_isOffline($is_off_line); // 是否离线
        if ($is_off_line) {
            $message->set_offlineExpireTime($offline_expire_time); // 离线时间
        }
        $message->set_data($template); // 设置推送消息类型
        $message->set_pushNetWorkType($network_type); // 设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送

        // 接收方
        $target = new \IGtTarget();
        $target->set_appId($this->app_id);
        $target->set_clientId($cid);

        // TODO 使用队列

        $rep = $this->igt->pushMessageToSingle($message, $target);

        return $rep;
    }

    /**
     * 多推接口案例
     */
    public function pushMessageToList($data)
    {
        putenv("gexin_pushList_needDetails=true");
        putenv("gexin_pushList_needAsync=true");

        // 解析数据
        $template_type = $data['template_type'];
        $template_data = $data['template_data'];
        $is_off_line = isset($data['template_data']['is_offline']) ? (bool)$data['template_data']['is_offline'] : $this->is_offline;
        $offline_expire_time = isset($data['template_data']['is_offline']) ? (int)$data['template_data']['offline_expire_time'] * 1000 * 3600 : $this->offline_expire_time * 1000 * 3600;
        $network_type = isset($data['template_data']['network_type']) ? (int)$data['template_data']['network_type'] : $this->network_type;

        // 消息模版
        $getui_template = new GetuiTemplate($template_type, $template_data);
        $template = $getui_template->getTemplate();

        // 个推信息体
        $message = new \IGtListMessage();
        $message->set_isOffline($is_off_line);
        if ($is_off_line) {
            $message->set_offlineExpireTime($offline_expire_time);
        }
        $message->set_data($template);
        $message->set_pushNetWorkType($network_type);
        //$contentId = $this->igt->getContentId($message, "toList任务别名功能"); // 根据TaskId设置组名，支持下划线，中文，英文，数字
        $contentId = $this->igt->getContentId($message);

        // 接收方列表
        foreach ($data['cid_list'] as $cid) {
            $target = new \IGtTarget();
            $target->set_appId($this->app_id);
            $target->set_clientId($cid);
            $targetList[] = $target;
        }

        // TODO 使用队列

        $rep = $this->igt->pushMessageToList($contentId, $targetList);

        return $rep;
    }

    /**
     * 群推接口案例
     */
    public function pushMessageToApp($data)
    {
        // 解析数据
        $template_type = $data['template_type'];
        $template_data = $data['template_data'];
        $is_off_line = isset($data['template_data']['is_offline']) ? (bool)$data['template_data']['is_offline'] : $this->is_offline;
        $offline_expire_time = isset($data['template_data']['is_offline']) ? (int)$data['template_data']['offline_expire_time'] * 1000 * 3600 : $this->offline_expire_time * 1000 * 3600;
        $network_type = isset($data['template_data']['network_type']) ? (int)$data['template_data']['network_type'] : $this->network_type;

        $getui_template = new GetuiTemplate($template_type, $template_data);
        $template = $getui_template->getTemplate();

        // 个推信息体
        $message = new \IGtAppMessage();
        $message->set_isOffline($is_off_line);
        if ($is_off_line) {
            $message->set_offlineExpireTime($offline_expire_time);
        }
        $message->set_pushNetWorkType($network_type);
        $message->set_data($template);
        $message->set_appIdList(array($this->app_id));

        // TODO 使用队列

        $rep = $this->igt->pushMessageToApp($message);

        return $rep;
    }
}